package com.kj.platformlogistics.home.adapter;

import android.graphics.Color;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.home.model.SolicilationedModel;
import com.kj.platformlogistics.home.model.StockListModel;

import java.util.List;

import me.samlss.broccoli.Broccoli;
import me.samlss.broccoli.BroccoliGradientDrawable;
import me.samlss.broccoli.PlaceholderParameter;

public class StockListAdapter extends BaseQuickAdapter<StockListModel.ResultDTO, BaseViewHolder> {

    public StockListAdapter(@Nullable List<StockListModel.ResultDTO> data) {
        super(R.layout.item_stock_list, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, StockListModel.ResultDTO item) {
        ImageView ivSelected=helper.getView(R.id.ivSelected);
        Broccoli broccoli = null;
        if (broccoli == null){
            broccoli = new Broccoli();
        }

        broccoli.removeAllPlaceholders();
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvOrderNumber))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPostAddress))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPostPhone))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvPostName))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.addPlaceholder(new PlaceholderParameter.Builder()
                .setView(helper.getView(R.id.tvNumber))
                .setDrawable(new BroccoliGradientDrawable(Color.parseColor("#DDDDDD"),
                        Color.parseColor("#CCCCCC"), 0, 1000, new LinearInterpolator()))
                .build());
        broccoli.show();


        Runnable task = null;
        if (task == null){
            final Broccoli finalBroccoli = broccoli;
            task = new Runnable() {
                @Override
                public void run() {
                    finalBroccoli.removeAllPlaceholders();

                    if (item==null){
                        return;
                    }

                    helper.addOnClickListener(R.id.ivSelected);
                    helper.setText(R.id.tvOrderNumber,"订单号："+item.getWaybillNumber());
                    helper.setText(R.id.tvPostAddress,item.getDetailAddress());
                    helper.setText(R.id.tvPostPhone,item.getPhone());
                    helper.setText(R.id.tvPostName,item.getName());
                    helper.setText(R.id.tvNumber,item.getNum()+"件");

                    if (item.getSelected()==null){
                        item.setSelected(false);
                    }
                    if (item.getShow()==null){
                        item.setShow(false);
                    }

                    if (item.getShow()){
                        ivSelected.setVisibility(View.VISIBLE);
                        if (item.getSelected()){
                            ivSelected.setImageResource(R.mipmap.icon_selected_dot);
                        }else {
                            ivSelected.setImageResource(R.mipmap.icon_unselected_dot);

                        }
                    }else {
                        ivSelected.setVisibility(View.GONE);
                    }
                    helper.addOnClickListener(R.id.tvAssignment);
                }
            };
        }else{
            helper.itemView.removeCallbacks(task);
        }
        helper.itemView.postDelayed(task, 1000);
    }
}
